// pages/open_code/open_code.js
const app = getApp()
var aliyun_api = require('../../utils/aliyun-api-gateway.js');
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    id: 'api',
    info: null,
    productKey: '',
    deviceName: '',
    userListInfo: [{
      icon: '',
      text: '指纹个数',
      usedNum: 0,
      totalNum: 100,
      detailInfo: [],
      isSpace: false,
      isArrow: true,
      isSwitch: false,
      isChecked: false,
      isunread: false,
      unreadNum: 1,
      isNum: true,
      isClick: true
    }, {
      icon: '',
      text: '密码个数',
      usedNum: 0,
      totalNum: 20,
      detailInfo: [],
      isSpace: false,
      isArrow: true,
      isSwitch: false,
      isChecked: false,
      isunread: false,
      unreadNum: 2,
      isNum: true,
      isClick: true
    }, {
      icon: '',
      text: '门卡个数',
      usedNum: 0,
      totalNum: 100,
      detailInfo: [],
      isSpace: false,
      isArrow: true,
      isSwitch: false,
      isChecked: false,
      isunread: false,
      unreadNum: 1,
      isNum: true,
      isClick: true
    }]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var data = app.globalData.kvStorage
    that.setData({
      info: data.deviceList[options.index],
      productKey: data.deviceList[options.index].productKey,
      deviceName: data.deviceList[options.index].deviceName
    })
    //  that.UpdateDevInfo();
    // that.UpdateInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.UpdateInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    that.UpdateInfo()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * radio change事件
   */
  radioChange: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    if (index > 3) {
      return;
    }

    var list = "userListInfo[" + index + "].isChecked";
    for (var i = 0; i < that.data.userListInfo.length; i++) {
      that.data.userListInfo[i].isChecked = false;
    }
    that.setData({
      userListInfo: that.data.userListInfo,
      [list]: true
    })
  },

  OpenDetailInfo: function (e) {
    var that = this
    util.NetStatusOk().then(function () {
      var index = e.currentTarget.dataset.index
      wx.navigateTo({
        //  url: './detailInfo/detailInfo?list=' + JSON.stringify(app.globalData.deviceInfo[index]) + '&text=' + title[index]
        url: './detailInfo/detailInfo?index=' + index + '&productKey=' + that.data.productKey + '&deviceName=' + that.data.deviceName
      })
    })
  },

  UpdateInfo: function () {
    var that = this
    that.getLockKeys()
      .then(function () {
        wx.stopPullDownRefresh()
      });
  },

  /***************************获取门锁钥匙*************************/
  getLockKeys: function () {
    var that = this;
    const params = {
      url: 'https://api.link.aliyun.com/thing/device/property/query',
      apiVer: '1.1.2',
      param: {
        // 接口参数
        "productKey": that.data.info.productKey,
        "deviceName": that.data.info.deviceName,
        "propertyIdentifier": "LockKeys"
      }
    }
    return new Promise(function (resolve, reject) {
      aliyun_api.request(params).then(function (res) {
        console.log(res.data.value)
        var index = 0
        var tmpUserList = that.data.userListInfo
        var tmp = new util.DevInfo()
        var deviceInfo = tmp.deviceInfo
        tmpUserList[0].usedNum = 0
        tmpUserList[1].usedNum = 0
        tmpUserList[2].usedNum = 0
        for (let i = 0; i < res.data.value.length; i++) {
          index = res.data.value[i].K
          switch (res.data.value[i].T) {
            case 1:
              tmpUserList[0].usedNum++
              deviceInfo[0].usedNum++
              deviceInfo[0].detailInfo[index] = {
                data: 1,
                backup: '',
                userLimit: res.data.value[i].L
              }
              
              break;
            case 2:
              tmpUserList[1].usedNum++
              deviceInfo[1].usedNum++
              deviceInfo[1].detailInfo[index] = {
                data: 1,
                backup: '',
                userLimit: res.data.value[i].L
              }
              break;
            case 3:
              tmpUserList[2].usedNum++
              deviceInfo[2].usedNum++
              deviceInfo[2].detailInfo[index] = {
                data: 1,
                backup: '',
                userLimit: res.data.value[i].L
              }
              break;
            default:
              break;
          }
        }
        that.setData({
          userListInfo: tmpUserList
        })

        var tmpdevInfo = wx.getStorageSync('deviceInfo')
        for (let j = 0; j < tmpdevInfo.deviceInfo.length; j++) {
          tmpdevInfo.deviceInfo[j].usedNum = deviceInfo[j].usedNum
          tmpdevInfo.deviceInfo[j].detailInfo = deviceInfo[j].detailInfo
        }
        tmpdevInfo.ifInited = true
        wx.setStorageSync('deviceInfo', tmpdevInfo)
        resolve()
      })
    })
  },
})